#include "chooselevelscene.h"
#include "mainscence.h"
#include <QPainter>
#include <QIcon>
#include <QMenuBar>
#include <QAction>
#include <QDebug>
#include <QObject>
#include "mypushbutton.h"
#include "playscene.h"
#include <QSound>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    QIcon icon(":/res/Coin0001.png");
    this->setWindowIcon(icon);
    this->setWindowTitle("选择关卡");
    MainScence* myp = dynamic_cast<MainScence*>(parent);
    if(myp)
    {   //setFixedSize 可以固定其大小，无法手动缩放
        //resize 指定大小，鼠标还可以手动缩放
        this->setFixedSize(myp->width(),myp->height());
    }
    //菜单栏
    QMenuBar* mbar = this->menuBar();
    //菜单
    QMenu* menu = mbar->addMenu("开始");
    //菜单下操作列表
    QAction *action = new QAction("返回");
    menu->addAction(action);
    //点击返回事件
    connect(action,&QAction::triggered,[=](){
        //this->close(); //也可以直接退出
        this->hide();
        MainScence* parent = dynamic_cast<MainScence*>(this->parent());
        if(parent)
        {
            parent->setGeometry(this->geometry());
            parent->show();
        }
    });
    //自定义返回按钮
    //返回按钮
    MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closeBtn->setParent(this);
    connect(closeBtn,&QPushButton::clicked,[=](){
        this->hide();
        MainScence* parent = dynamic_cast<MainScence*>(this->parent());
        if(parent)
        {
            parent->show();
        }
    });
    closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
    drawLevelBtns();
    qDebug()<<"before paintEvent";
}
void ChooseLevelScene::paintEvent(QPaintEvent* ev)
{
    //qDebug()<<"paint";
    //创建画家
    QPainter painter(this);
    //创建QPixmap对象
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height()+this->menuBar()->height(), pix);
    //加载标题
   pix.load(":/res/Title.png");
   painter.drawPixmap( (this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);

}
void ChooseLevelScene::drawLevelBtns()
{
    int x = this->width()/5;
    //选择关卡按钮音效
    QSound *chooseSound = new QSound(":/res/TapButtonSound.wav",this);
    for(int i =0;i<20;i++)
    {
        //创建关卡按钮
        MyPushButton* tempbtn = new MyPushButton(":/res/LevelIcon.png");
        tempbtn->setParent(this);
        tempbtn->move(70+(i%4)*x , 150+ (i/4)*90);
        tempbtn->setLabelText(QString::number(i+1));
        //每个按钮点击以后打开一个新场景，进入关卡
        connect(tempbtn,&QPushButton::clicked,[=](){
            PlayScene * pscene = new PlayScene(this);
            pscene->setGeometry(this->geometry());
            pscene->setAttribute(Qt::WidgetAttribute::WA_DeleteOnClose);
            pscene->setLevel(i+1);
            pscene->initScene();
            pscene->show();
            chooseSound->play();
        });

    }
}
